package cn.xny.txcc.web.controller;

import cn.xny.txcc.dto.CouresDetailVo;
import cn.xny.txcc.dto.CouresDto;
import cn.xny.txcc.result.JSONResult;
import cn.xny.txcc.result.PageList;
import cn.xny.txcc.service.ICourseService;
import cn.xny.txcc.domain.Course;
import cn.xny.txcc.query.CourseQuery;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody CouresDto courseDto){


        courseService.saveCourse(courseDto);

        return JSONResult.success();
    }



    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());



        page = courseService.selectPage(page);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }

    //上架
    @RequestMapping(value = "/onLineCourse/{id}",method = RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("id") Long id){

       courseService.onLineCourse(id);


       return JSONResult.success();

    }

     @GetMapping("detail/data/{id}")
     public JSONResult getCourseDetail(@PathVariable("id") Long id){

                CouresDetailVo  vo = courseService.getCourseDetail(id);

                return JSONResult.success(vo);
     }

    @GetMapping(value="/getForUser/{id}")
    public JSONResult getForUser(@PathVariable("id") Long id){
        String fileUrl =  courseService.getForUser(id);

        return JSONResult.success(fileUrl);
    }
}
